﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.Views.Pages
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Shell.Tools;
    using Probel.Ldoc.ViewModels.Pages;
    using Probel.Ldoc.ViewModels.ToolBoxes;
    using Probel.Ldoc.Views.Helpers;
    using Probel.Ldoc.Views.Properties;
    using Probel.Ldoc.Views.ToolBoxes;
    using Probel.Mvvm.Gui;
    using Probel.Mvvm.Gui.FileServices;

    using SelectedImage = System.Drawing.Image;

    /// <summary>
    /// Interaction logic for EditPatientView.xaml
    /// </summary>
    public partial class PatientEditorView : Page, ISavable, IInitialisable
    {
        #region Fields

        public static RoutedCommand UndoCommand = new RoutedCommand();

        #endregion Fields

        #region Constructors

        public PatientEditorView(PatientEditorViewModel viewmodel)
        {
            InitializeComponent();

            this.professionContent.Closing += (sender, e) => this.professionPopup.IsOpen = false;
            this.reputationContent.Closing += (sender, e) => this.reputationPopup.IsOpen = false;
            this.insuranceContent.Closing += (sender, e) => this.insurancePopup.IsOpen = false;
            this.doctorContent.Closing += (sender, e) => this.doctorPopup.IsOpen = false;

            this.DataContext = viewmodel;
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets a value indicating whether this instance has been modified.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is modified; otherwise, <c>false</c>.
        /// </value>
        public bool IsModified
        {
            get { return this.ViewModel.IsModified; }
        }

        private PatientEditorViewModel ViewModel
        {
            get
            {
                return this.DataContext as PatientEditorViewModel;
            }
        }

        #endregion Properties

        #region Methods

        public void Initialise()
        {
            this.ViewModel.Refresh();
        }

        /// <summary>
        /// Saves the unchanged data of this instance.
        /// </summary>
        /// <returns>
        ///   <c>True</c> if navigation should be cancelled. Otherwise <c>False</c>
        /// </returns>
        public bool ManageSaveFlow()
        {
            var save = ViewService.MessageBox.Question(Messages.Msg_AskSavePatient);

            if (save) { this.ViewModel.UpdatePatient(); }

            return false; //So far, cancellation is not supported...
        }

        private void BindTag()
        {
            if (!string.IsNullOrWhiteSpace(this.tagBox.Text))
            {
                this.ViewModel.BindTag(this.tagBox.Text);
                this.tagBox.Text = string.Empty;
            }
        }

        private void Click_AddDoctor(object sender, RoutedEventArgs e)
        {
            this.ViewModel.UpdatePatient();

            this.doctorPopup.IsOpen = true;

            this.doctorContent.FocusTextBox();

            this.doctorPopup.DataContext = this.ViewModel.BuildDoctorPopupViewModel();
        }

        private void Click_AddInsurance(object sender, RoutedEventArgs e)
        {
            this.insurancePopup.IsOpen = true;

            this.insuranceContent.FocusTextBox();

            this.insurancePopup.DataContext = this.ViewModel.BuildInsurancePopupViewModel();
        }

        private void Click_AddPractice(object sender, RoutedEventArgs e)
        {
            this.practicePopup.IsOpen = true;

            this.practiceContent.FocusTextBox();

            this.practicePopup.DataContext = this.ViewModel.BuildPracticePopupViewModel();
        }

        private void Click_AddProfession(object sender, RoutedEventArgs e)
        {
            this.professionPopup.IsOpen = true;

            this.professionContent.FocusTextBox();

            this.professionPopup.DataContext = this.ViewModel.BuildProfessionPopupViewModel();
        }

        private void Click_AddReputation(object sender, RoutedEventArgs e)
        {
            this.reputationPopup.IsOpen = true;

            this.reputationContent.FocusTextBox();

            this.reputationPopup.DataContext = this.ViewModel.BuildReputationPopupViewModel();
        }

        private void Click_AddTag(object sender, RoutedEventArgs e)
        {
            this.ViewModel.RefreshTags();
            this.tagPopup.IsOpen = true;
        }

        private void Click_CreateDoctor(object sender, RoutedEventArgs e)
        {
            var view = new CreateDoctorView(this.ViewModel.BuildCreateDoctorViewModel());
            view.Owner = ViewService.RootWindow;
            view.ShowDialog();
        }

        private void Click_Hyperlink(object sender, RoutedEventArgs e)
        {
            var path = string.Empty;
            var hasAccepted = FileGuiFactory.Win32.SelectFile(p => path = p);
            if (hasAccepted.HasValue && hasAccepted.Value)
            {
                Mouse.OverrideCursor = Cursors.Wait;
                var img = SelectedImage.FromFile(path);
                this.ViewModel.Thumbnail = img.GetThumbnail();
                Mouse.OverrideCursor = Cursors.Arrow;
            }
        }

        private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.ViewModel.UpdatePatient();
        }

        private void SaveCommand__CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void Selected_TagToAdd(object sender, RoutedEventArgs e)
        {
            if (this.tagListBox.SelectedItem is TagDto)
            {
                this.tagPopup.IsOpen = false;

                var tag = this.tagListBox.SelectedItem as TagDto;
                this.ViewModel.BindTag(tag);
            }
        }

        private void UndoCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = (this.ViewModel != null)
                ? this.ViewModel.HasPatient
                : false;
        }

        private void UndoCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (ViewService.MessageBox.Question(Messages.Msg_AskRevertPatientData)) { this.ViewModel.RevertPatient(); }
        }

        private void WatermarkTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter) { this.BindTag(); }
        }

        #endregion Methods
    }
}